/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package facade;

import ejb.*;
import ejb.Util;
import java.util.*;
import javax.ejb.*;

/**
 *
 * @author Usuario
 */
public class SystemViewBean implements SessionBean {
    
  private SessionContext context;
    
    // <editor-fold defaultstate="collapsed" desc="EJB infrastructure methods. Click the + sign on the left to edit the code.">;

    // TODO Add code to acquire and use other enterprise resources (DataSource, JMS, enterprise bean, Web services)
    // TODO Add business methods or web service operations

    /**
     * @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)
     */
    public void setSessionContext(SessionContext aContext) {
        context = aContext;
    }
    
    /**
     * @see javax.ejb.SessionBean#ejbActivate()
     */
    public void ejbActivate() {
        
    }
    
    /**
     * @see javax.ejb.SessionBean#ejbPassivate()
     */
    public void ejbPassivate() {
        
    }
    
    /**
     * @see javax.ejb.SessionBean#ejbRemove()
     */
    public void ejbRemove() {
        
    }
    
    // </editor-fold>;

  public void ejbCreate() { }

  public Collection getClientes() {
    try {
      Vector v = new Vector();
      Collection c = Util.getClienteHome().findAllItems();

      for (Iterator it = c.iterator(); it.hasNext();)
        v.add(new ClienteFacade((ClienteLocal)it.next()));

      return v;
    } catch (Exception e) {
      return null;
    }
  }

  public Collection getMensagens() {
    try {
      Vector v = new Vector();
      Collection c = Util.getMensagemHome().findAllItems();

      for (Iterator it = c.iterator(); it.hasNext();)
        v.add(new MensagemFacade((MensagemLocal)it.next()));

      return v;
    } catch (Exception e) {
      return null;
    }
  }

  public Collection getProjetos() {
    try {
      Vector v = new Vector();
      Collection c = Util.getProjetoHome().findAllItems();

      for (Iterator it = c.iterator(); it.hasNext();)
        v.add(new ProjetoFacade((ProjetoLocal)it.next()));

      return v;
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }

  public Collection getCargasSemanaisFuncionario() {
    try {
      Vector v = new Vector();
      Collection c = Util.getCargaSemanalFuncionarioHome().findAllItems();

      for (Iterator it = c.iterator(); it.hasNext();)
        v.add(new CargaSemanalFuncionarioFacade((CargaSemanalFuncionarioLocal)it.next()));

      return v;
    } catch (Exception e) {
      return null;
    }
  }

  public Collection getCargasSemanaisProjeto() {
    try {
      Vector v = new Vector();
      Collection c = Util.getCargaSemanalProjetoHome().findAllItems();

      for (Iterator it = c.iterator(); it.hasNext();)
        v.add(new CargaSemanalProjetoFacade((CargaSemanalProjetoLocal)it.next()));

      return v;
    } catch (Exception e) {
      return null;
    }
  }

  public Collection getFaturas() {
    try {
      Vector v = new Vector();
      Collection c = Util.getFaturaHome().findAllItems();

      for (Iterator it = c.iterator(); it.hasNext();)
        v.add(new FaturaFacade((FaturaLocal)it.next()));

      return v;
    } catch (Exception e) {
      return null;
    }
  }

  public Collection getFuncionarios() {
    try {
      Vector v = new Vector();
      Collection c = Util.getFuncionarioHome().findAllItems2();

      for (Iterator it = c.iterator(); it.hasNext();)
        v.add(new FuncionarioFacade((FuncionarioLocal)it.next()));

      return v;
    } catch (Exception e) {
      return null;
    }
  }

  public Collection getGerentes() {
    try {
      Vector v = new Vector();
      Collection c = Util.getGerenteHome().findAllItems();

      for (Iterator it = c.iterator(); it.hasNext();)
        v.add(new GerenteFacade((GerenteLocal)it.next()));

      return v;
    } catch (Exception e) {
      return null;
    }
  }

  public Collection getRequerimentosHoraExtra() {
    try {
      Vector v = new Vector();
      Collection c = Util.getRequerimentoHoraExtraHome().findAllItems2();

      for (Iterator it = c.iterator(); it.hasNext();)
        v.add(new RequerimentoHoraExtraFacade((RequerimentoHoraExtraLocal)it.next()));

      return v;
    } catch (Exception e) {
      return null;
    }
  }

    public void adicionarCliente(String nome) {
        try {
            Util.getClienteHome().create(nome);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void adicionarGerente(String nome) {
        try {
            Util.getGerenteHome().create(nome);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void adicionarProjeto(String id, String idGerente, int cargaHorariaTotal,
            int limiteCargaHorExtra, int limiteCargaHorExtraPorFunc, int precoHora) {
        try {
            ProjetoLocal proj = Util.getProjetoHome().create(id);
            proj.setCargaHorariaTotal(cargaHorariaTotal);
            proj.setIdGerente(idGerente);
            proj.setLimiteCargaExtra(limiteCargaHorExtra);
            proj.setLimiteCargaExtraPorFuncionario(limiteCargaHorExtraPorFunc);
            proj.setPrecoPorHora(precoHora);
        } catch (CreateException ex) {
            ex.printStackTrace();
        }
    }

    public ProjetoFacade getProjeto(String id) {
        try {
            return (new ProjetoFacade(Util.getProjetoHome().findByPrimaryKey(id)));
        } catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

  public void removerFuncionario(String idFuncionario) {
    try {
      Util.getFuncionarioHome().findByPrimaryKey(idFuncionario).remove();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public void adicionarFuncionario(String id, String idProjeto) {
    try {
      FuncionarioLocal f = Util.getFuncionarioHome().create(id);
      f.setIdProjeto(Util.getProjetoHome().findByPrimaryKey(idProjeto));
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

    public void adicionarClienteProjeto(String idCliente, String idProjeto) {
        try {
            ProjetoLocal proj = Util.getProjetoHome().findByPrimaryKey(idProjeto);
            proj.setIdCliente(Util.getClienteHome().findByPrimaryKey(idCliente));
        } catch (FinderException ex) {
            ex.printStackTrace();
        }
    }

    public void removerProjeto(String projetp) {
        try {
            Util.getProjetoHome().remove(projetp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
